﻿using System.Collections.Generic;
using Bio.Views;
using BioBrowser.Models;
using JulMar.Windows;
using JulMar.Windows.Mvvm;
using Microsoft.Win32;
using System.IO;

/* 
* Copyright (c) 2009, The University of Texas at Austin
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, 
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, 
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, 
* this list of conditions and the following disclaimer in the documentation and/or other materials 
* provided with the distribution.
*
* Neither the name of The University of Texas at Austin nor the names of its contributors may be 
* used to endorse or promote products derived from this software without specific prior written 
* permission.
* 
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR 
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS 
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

namespace BioBrowser.ViewModels
{
    /// <summary>
    /// This is the top-level workspace category in our window
    /// </summary>
    public class WorkspaceRootViewModel : SelectionViewModel
    {
        private const string WORKSPACE_DATA = @"workspaces.xml";

        public MTObservableCollection<WorkspaceViewModel> Children { get; private set; }

        /// <summary>
        /// Constructor for the BioCategory
        /// </summary>
        public WorkspaceRootViewModel()
        {
            RegisterWithMessageMediator();
            Children = new MTObservableCollection<WorkspaceViewModel>();
        }

        /// <summary>
        /// This populates the context menu for the category
        /// </summary>
        public override List<MenuItem> MenuOptions
        {
            get
            {
                IsSelected = true;
                return new List<MenuItem>
                    {
                        new MenuItem("Create _New Workspace") {Command = new DelegatingCommand(OnNewWorkspace)},
                        new MenuItem("Open _Workspace") { Command = new DelegatingCommand(OnOpenWorkspace)} 
                    };
            }
        }

        /// <summary>
        /// This method opens an existing workspace
        /// </summary>
        private void OnOpenWorkspace()
        {
            OpenFileDialog ofd = new OpenFileDialog { Filter = "Bio Workspaces|*"+Workspace.WS_EXTENSION };
            if (ofd.ShowDialog().Value)
            {
                Children.Add(new WorkspaceViewModel(Workspace.Load(ofd.FileName)));
                IsExpanded = true;
            }
        }

        /// <summary>
        /// This method creates a new workspace
        /// </summary>
        private void OnNewWorkspace()
        {
            SaveFileDialog sfd = new SaveFileDialog
                 {
                     CheckPathExists = true,
                     AddExtension = true,
                     Filter = "Bio Workspaces|*" + Workspace.WS_EXTENSION,
                     RestoreDirectory = true,
                     DefaultExt = Workspace.WS_EXTENSION,
                     OverwritePrompt = true,
                     Title = "Select the name and location of the new Workspace to create."
                 };
            bool? result = sfd.ShowDialog();
            if (result.Value)
            {
                Workspace ws = new Workspace {Filename = sfd.FileName};
                ws.Name = Path.GetFileNameWithoutExtension(ws.Filename);
                Children.Add(new WorkspaceViewModel(ws));
                IsExpanded = true;
            }
        }

        [MessageMediatorTarget(ViewMessages.RemoveWorkspace)]
        private void OnRemoveWorkspace(object parameter)
        {
            WorkspaceViewModel vm = (WorkspaceViewModel)parameter;
            Children.Remove(vm);
        }

        protected override void Dispose(bool isDisposing)
        {
            if (isDisposing)
            {
                foreach (var ws in Children)
                    ws.Save();
            }

            base.Dispose(isDisposing);
        }

    }
}